package com.wl.service.impl;

import java.util.List;

import com.fasterxml.jackson.databind.util.BeanUtil;
import com.wl.dao.AccountDao;
import com.wl.dao.UserDao;
import com.wl.exception.BusinessException;
import com.wl.enums.Status;
import com.wl.enums.UserType;
import com.wl.model.Account;
import com.wl.model.User;
import com.wl.utils.PageUtil;
import com.wl.vo.UserAccount;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.wl.service.UserService;
import org.springframework.transaction.annotation.Transactional;


@Service("userServiceImpl")
public class UserServiceImpl implements UserService {

    private final static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private UserDao userDao;

    @Autowired
    private AccountDao accountDao;


    @Override
    public List<User> userList() {
        return userDao.selectList(new EntityWrapper<User>());
    }

    private Account getAccount(String loginName, String password) {
        EntityWrapper<Account> accountWrapper = new EntityWrapper<Account>();
        accountWrapper.eq("login_name", loginName);
        if (!password.isEmpty()) {
            accountWrapper.eq("password", password);
        }
        List<Account> accountList = accountDao.selectList(accountWrapper);
        if (accountList.size() > 0) {
            return accountList.get(0);
        }
        return null;
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public User register(UserAccount userAccount) {
        Account ac = getAccount(userAccount.getLoginName(), userAccount.getPassword());
        if (ac != null) {
            //账号被注册 //TODO
        }
        Account account = new Account();
        account.setLoginName(userAccount.getLoginName());
        account.setPassword(userAccount.getPassword());

        final List<Account> accountList = accountDao.selectList(new EntityWrapper<Account>().eq("login_name", userAccount.getLoginName()));
        if(!accountList.isEmpty()){
            throw new BusinessException("用户已存在","register");
        }

        accountDao.insert(account);

        User user = new User();
        user.setAccountId(account.getAccountId());
        user.setName(account.getLoginName());
        user.setType(UserType.common.name());
        user.setHeadPhoto(userAccount.getUser().getHeadPhoto());
        user.setStatus(Status.enabled.name());
        user.setSign(userAccount.getUser().getSign());
        user.setTel(userAccount.getUser().getTel());
        user.setQq(userAccount.getUser().getQq());
        user.setEmail(userAccount.getUser().getEmail());
        userDao.insert(user);
        return user;
    }

    @Override
    public User login(UserAccount userAccount) {
        EntityWrapper<Account> accountWrapper = new EntityWrapper<Account>();
        accountWrapper.eq("login_name", userAccount.getLoginName());
        //密码不加密
        accountWrapper.eq("password", userAccount.getPassword());
        List<Account> accountList = accountDao.selectList(accountWrapper);
        if (accountList.size() > 0) {
            Account account = accountList.get(0);

            EntityWrapper<User> userWrapper = new EntityWrapper<User>();
            userWrapper.eq("account_id", account.getAccountId());
            userWrapper.eq("status", Status.enabled.name());
            List<User> userList = userDao.selectList(userWrapper);
            if (userList.size() == 0) {
                throw new BusinessException("用户已被禁用或没有用户信息，请重新注册","login");
                //用户已被禁用或没有用户信息，请重新注册 //TODO
            }
            return userList.get(0);
            //登录成功 //TODO
        } else {
            throw new BusinessException("用户名或密码不正确","login");
            //用户名或密码不正确 //TODO
        }
    }

    /**
     * 用户管理列表
     */
    @Override
    public List<UserAccount> userAccountList(Integer page, Integer pageSize) {
        List<UserAccount> list = userDao.userAccountList(page, pageSize);
        return list;
    }


    /***
     * 用户信息
     * */
    @Override
    public User getUser(long userId) {
        return userDao.selectById(userId);
    }


    @Override
    public List<User> getLimitUser(PageUtil pageUtil) {
        return userDao.getLimitUser(pageUtil);
    }

    @Override
    public Integer getCountUser() {
        return userDao.getCountUser();
    }

    @Override
    public void deleteUser(long userId) {
        userDao.deleteById(userId);
    }

    @Override
    public void addUser(User user) {
        userDao.insert(user);
    }

    @Override
    public void updateUser(User user) {

        userDao.updateById(user);
    }

    @Override
    public List<User> findByUser(User user) {
        if (user.getName().trim().isEmpty()){
            user.setName(null);
        }
        if (user.getTel().trim().isEmpty()) {
            user.setTel(null);
        }
        return userDao.findByUser(user);
    }


    /**
     * 删除用户，admin管理员才能删除
     */
    @Override
    @Transactional
    public void disabledUser(Long userId) {
        User user = userDao.selectById(userId);
        user.setStatus(Status.disabled.name());
        userDao.updateById(user);
    }
}
